package com.showde.shoppingmall.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.showde.shoppingmall.db.constant.ProductAHRConstant;
import com.showde.shoppingmall.domain.ProductAHR;

public class ProductAHRDBCommand implements
		DBCommandInteface<ProductAHR, String> {

	private static final String TAG = "ProductDBCommand";

	/**
	 * 广告商品
	 */
	public static final String TYPE_ADVERTISEMENT = "1";
	/**
	 * 热门商品
	 */
	public static final String TYPE_HOT = "1";

	DBSession dbSession;

	public ProductAHRDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context));
	}

	@Override
	public void add(ProductAHR ProductAHR) {
		dbSession.insert(ProductAHRConstant.TABLENAME,
				getProductContentValue(ProductAHR),
				SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void add(List<ProductAHR> products) {

		List<ContentValues> contentValuesList = new ArrayList<ContentValues>();
		for (ProductAHR ProductAHR : products) {
			contentValuesList.add(getProductContentValue(ProductAHR));
		}
		dbSession.insertBatch(ProductAHRConstant.TABLENAME, contentValuesList,
				SQLiteDatabase.CONFLICT_REPLACE);

	}

	@Override
	public void delete(String pk) {

	}

	@Override
	public void delete(SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public void modify(SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public void modify(String pk, SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public ProductAHR query(final String PK) {
		final ProductAHR ProductAHR = new ProductAHR();
		dbSession.query(ProductAHRConstant.TABLENAME,
				ProductAHRConstant.dbColumns, ProductAHRConstant.PRODUCTID
						+ "=?", new String[] { PK }, null, null, null,
				new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format("query[%s]", PK));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							while (cursor.moveToNext()) {
								getProductFromCursor(ProductAHR, cursor);
							}
						}
					}
				});
		return ProductAHR;
	}

	@Override
	public List<ProductAHR> query(SQLiteCriteria sqLiteCriteria) {
		return null;
	}

	@Override
	public List<ProductAHR> queryAll() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 根据商品类别ID获取商品列表
	 * 
	 * @param productCategoryId
	 * @return
	 */
	public List<ProductAHR> queryProductByCategoryId(
			final String productCategoryId) {

		final List<ProductAHR> products = new ArrayList<ProductAHR>();

		dbSession.query("select * from " + ProductAHRConstant.TABLENAME
				+ " where " + ProductAHRConstant.PRODUCTCATEGORYID + " =?",
				new String[] { productCategoryId },
				new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format(
								"queryProductByCategoryId[%s]:%s",
								productCategoryId, msg));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							ProductAHR ProductAHR = null;
							while (cursor.moveToNext()) {
								ProductAHR = new ProductAHR();
								getProductFromCursor(ProductAHR, cursor);
								products.add(ProductAHR);
							}
						}
					}
				});

		return products;
	}

	/**
	 * 获取广商品
	 * 
	 * @return
	 */
	public List<ProductAHR> queryProductAdvertisement() {
		final List<ProductAHR> products = new ArrayList<ProductAHR>();
		dbSession.query(
				"select * from " + ProductAHRConstant.TABLENAME + " where "
						+ ProductAHRConstant.PRODUCTISADVERTISEMENT + " =?",
				new String[] { TYPE_ADVERTISEMENT },
				new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format(
								"queryProductAdvertisement[%s]:%s",
								TYPE_ADVERTISEMENT, msg));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							ProductAHR ProductAHR = null;
							while (cursor.moveToNext()) {
								ProductAHR = new ProductAHR();
								getProductFromCursor(ProductAHR, cursor);
								products.add(ProductAHR);
							}
						}
					}
				});

		return products;
	}

	/**
	 * 获取热门商品
	 * 
	 * @return
	 */
	public List<ProductAHR> queryProductHot() {
		final List<ProductAHR> products = new ArrayList<ProductAHR>();
		dbSession.query("select * from " + ProductAHRConstant.TABLENAME
				+ " where " + ProductAHRConstant.PRODUCTISHOT + " =?",
				new String[] { TYPE_HOT }, new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format("queryProductHot[%s]:%s",
								TYPE_HOT, msg));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							ProductAHR ProductAHR = null;
							while (cursor.moveToNext()) {
								ProductAHR = new ProductAHR();
								getProductFromCursor(ProductAHR, cursor);
								products.add(ProductAHR);
							}
						}
					}
				});

		return products;
	}

	/**
	 * 按照商品类型获取商品信息，如热门，推荐，人气...
	 * 
	 * @param productCategoryId
	 * @return
	 */
	public List<ProductAHR> queryProductByType(final String productType) {

		final List<ProductAHR> products = new ArrayList<ProductAHR>();

		dbSession.query("select * from " + ProductAHRConstant.TABLENAME
				+ " where " + ProductAHRConstant.PRODUCTCATEGORYID + " =?",
				new String[] { productType }, new QueryCompleteCallback() {

					@Override
					public void onQueryError(String msg) {
						Log.d(TAG, String.format("queryProductByType[%s]:%s",
								productType, msg));
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor != null && cursor.getCount() > 0) {
							ProductAHR ProductAHR = null;
							while (cursor.moveToNext()) {
								ProductAHR = new ProductAHR();
								getProductFromCursor(ProductAHR, cursor);
								products.add(ProductAHR);
							}
						}
					}
				});

		return products;
	}

	/**
	 * 获取商品ContentValue
	 * 
	 * @param ProductAHR
	 * @return
	 */
	private ContentValues getProductContentValue(ProductAHR productAHR) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(ProductAHRConstant.PRODUCTID,
				productAHR.getProductID());
		contentValues.put(ProductAHRConstant.PRODUCTNAME,
				productAHR.getProductName());
		contentValues.put(ProductAHRConstant.PRODUCTTITLE,
				productAHR.getProductTitle());
		contentValues.put(ProductAHRConstant.PRODUCTLOGO,
				productAHR.getProductLogo());
		contentValues.put(ProductAHRConstant.PRODUCTPRICE,
				productAHR.getProductPrice());
		contentValues.put(ProductAHRConstant.PRODUCTCATEGORYID,
				productAHR.getProductCategoryID());
		contentValues.put(ProductAHRConstant.PRODUCTISHOT,
				productAHR.getIsHot());
		contentValues.put(ProductAHRConstant.PRODUCTISADVERTISEMENT,
				productAHR.getIsAdvertisement());
		contentValues.put(ProductAHRConstant.PRODUCTRANK, productAHR.getRank());
		return contentValues;
	}

	private void getProductFromCursor(ProductAHR productAHR, Cursor cursor) {
		productAHR.setProductID(dbSession.getString(cursor,
				ProductAHRConstant.PRODUCTID));
		productAHR.setProductName(dbSession.getString(cursor,
				ProductAHRConstant.PRODUCTNAME));
		productAHR.setProductTitle(dbSession.getString(cursor,
				ProductAHRConstant.PRODUCTTITLE));
		productAHR.setProductLogo(dbSession.getString(cursor,
				ProductAHRConstant.PRODUCTLOGO));
		productAHR.setProductPrice(dbSession.getFloat(cursor,
				ProductAHRConstant.PRODUCTPRICE));
		productAHR.setProductCategoryID(dbSession.getString(cursor,
				ProductAHRConstant.PRODUCTCATEGORYID));
	}

}
